package com.administrator.shiniang;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.administrator.shiniang.adapter.TextWatcherAdapter;
import com.rockerhieu.emojicon.EmojiconEditText;
import com.rockerhieu.emojicon.EmojiconGridFragment;
import com.rockerhieu.emojicon.EmojiconTextView;
import com.rockerhieu.emojicon.EmojiconsFragment;
import com.rockerhieu.emojicon.emoji.Emojicon;


import java.util.Timer;
import java.util.TimerTask;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class EmojiActivity extends FragmentActivity implements EmojiconGridFragment.OnEmojiconClickedListener, EmojiconsFragment.OnEmojiconBackspaceClickedListener
        , View.OnClickListener {

    @InjectView(R.id.textEmojicon)
    EmojiconTextView mTextEmojicon;
    @InjectView(R.id.editEmojicon)
    EmojiconEditText mEditEmojicon;
    @InjectView(R.id.icon_expression)
    ImageView mIconExpression;
    @InjectView(R.id.ll_expression)
    LinearLayout mLlExpression;


    private TranslateAnimation mShowAnimation;
    private TranslateAnimation mHideAnimation;
    private View mEmojicons;
    private boolean isShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_emoji);
        ButterKnife.inject(this);

        mEditEmojicon.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mTextEmojicon.setText(s);
            }
        });

        mEditEmojicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEmojicons.setVisibility(View.GONE);
                ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(mEditEmojicon, 0);//显示键盘
            }
        });

        initView();
        initAnimation();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(mEditEmojicon, InputMethodManager.SHOW_FORCED);//显示键盘
            }
        }, 100);

    }

    private void initView() {
        mIconExpression.setOnClickListener(this);
        mEmojicons = findViewById(R.id.emojicons);
        mEmojicons.setVisibility(View.GONE);
        isShow = false;
    }

    public void onClick(View v) {

        if (v.getId() == R.id.icon_expression) {
            if (isShow) {
                isShow = false;
                mEmojicons.startAnimation(mHideAnimation);
                mEmojicons.setVisibility(View.GONE);
                ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(mEditEmojicon, 0);//显示键盘
            } else {
                isShow = true;
                //
                mLlExpression.setVisibility(View.GONE);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mLlExpression.setVisibility(View.VISIBLE);
                    }
                }, 400);
                mEmojicons.startAnimation(mShowAnimation);
                mEmojicons.setVisibility(View.VISIBLE);
                ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(),0);//强制隐藏键盘
            }
        }
    }

    @Override
    public void onEmojiconClicked(Emojicon emojicon) {
        EmojiconsFragment.input(mEditEmojicon, emojicon);
    }

    @Override
    public void onEmojiconBackspaceClicked(View v) {
        EmojiconsFragment.backspace(mEditEmojicon);

    }

    private void initAnimation() {
        // 从自已-1倍的位置移到自己原来的位置
        mShowAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        mHideAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f);
        mShowAnimation.setDuration(500);
        mHideAnimation.setDuration(500);

    }
}
